# -*- coding: UTF-8 -*-
#
# FileName     : response
# Author       : EastsunW eastsunw@foxmail.com
# Create at    : 2024-01-18 15:49
# Last Modified: 2024-01-18 15:49
# Modified By  : EastsunW
# -------------
# Description  :
# -------------

import io
import os
import gzip
from flask_restful import Resource, reqparse
from flask import send_file, make_response


class Download_Class(Resource):
    def get(self):
        base_dir = os.path.abspath(os.path.dirname(__file__))
        parser = reqparse.RequestParser()
        parser.add_argument(
            'type', type=str,
            choices=['coding', 'lncRNA', 'all'],
            required=True,
            default='all',
            nullable=False
        )
        argument = parser.parse_args()
        if argument['type'] == 'coding':
            file_name = 'mnvtf_coding.txt.gz'
        elif argument['type'] == 'lncRNA':
            file_name = 'mnvtf_lncRNA.txt.gz'
        else:
            # file_name = 'test.txt.gz'
            file_name = 'mnvtf_all.tar.gz'
        compressed_file = io.BytesIO()
        with gzip.GzipFile(
            fileobj=compressed_file,
            mode='wb'
        ) as f:
            with open(os.path.join(base_dir, file_name), 'rb') as f2:
                f.write(f2.read())
        return send_file(
            os.path.join(base_dir, file_name),
            as_attachment=True
        )
